package com.hpin.fansea.service;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.HouseRentAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.HouseRentBean;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.sale.SaleDetailActivity;
import com.hpin.fansea.sale.SaleReleaseActivity;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.hpin.fansea.widget.ActionSheetDialog;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 房屋租售
 * Created by Aaron on 15/9/16.
 */
public class HouseRentActivity extends BaseActivity {

    private HouseRentAdapter mAdapter;
    private TextView tv_sale;
    private TextView tv_rent;
    private PullToRefreshListView plv_rent;

    private List<HouseRentBean.HouseRentInfo> houseRentList = new ArrayList<>();

    private String currentStyle = Constant.HOUSESELL;//当前显示的是出售还是出租  sell 出售 rent 出租
//    private

    private Intent mIntent;

    private int sellIndex;
    private int rentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_house_rent);

        initWidget();

        getHouseList(1, currentStyle);

    }

    /**
     * 获取房屋列表
     * @param pageIndex 第几页
     * @param type  sell 出售  rent 出租
     */
    private void getHouseList(int pageIndex, String type) {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.HOUSERENTSERVICE + "?token="
                        + sp.getString(Constant.token, "") + "&pageSize=" + Constant.PAGECOUNT + "&pageIndex=" + pageIndex + "&type=" + type,
                new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        plv_rent.onRefreshComplete();

                        try {

                            HouseRentBean houseRentBean = JSONObject.parseObject(str, HouseRentBean.class);

                            if (Constant.STATUS_SUCESS.equals(houseRentBean.status)) {

                                List<HouseRentBean.HouseRentInfo> tempResult = houseRentBean.result;

                                if (tempResult == null)
                                    return;

                                setDataView(tempResult);

                                mAdapter.setHouseRentInfos(houseRentList);
                                mAdapter.notifyDataSetChanged();

                            } else {
                                showToast(houseRentBean.message);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, new HttpUtil.FailListener() {
                    @Override
                    public void setOnFailListener() {
                        plv_rent.onRefreshComplete();
                    }
                });

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.house_rent));
        tv_right.setText(getResources().getString(R.string.release));

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        plv_rent = (PullToRefreshListView) findViewById(R.id.plv_rent);

        mAdapter = new HouseRentAdapter(this, houseRentList);
        plv_rent.setAdapter(mAdapter);

        tv_sale = (TextView) findViewById(R.id.tv_sale);
        tv_rent = (TextView) findViewById(R.id.tv_rent);

        tv_sale.setOnClickListener(this);
        tv_rent.setOnClickListener(this);

        plv_rent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (houseRentList==null || houseRentList.size() == 0)
                    return;

                --position;

                if (Constant.HOUSESELL.equals(currentStyle)) {//出售详情

                    mIntent = new Intent(HouseRentActivity.this, SaleDetailActivity.class);

                }else {                 //出租详情

                    mIntent = new Intent(HouseRentActivity.this, RentDetailActivity.class);
                }
                //房屋id
                mIntent.putExtra("houseRentId", houseRentList.get(position).id_);
                startActivity(mIntent);

            }
        });

        plv_rent.setMode(PullToRefreshBase.Mode.BOTH);

        plv_rent.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lastUpdate(refreshView);

                rentIndex = 1;
                sellIndex = 1;

                if (Constant.HOUSESELL.equals(currentStyle)) {
                    getHouseList(sellIndex, currentStyle);
                }else {
                    getHouseList(rentIndex, currentStyle);
                }

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (Constant.HOUSESELL.equals(currentStyle)) {
                    getHouseList(++sellIndex, currentStyle);
                }else {
                    getHouseList(++rentIndex, currentStyle);
                }
            }
        });

    }

    protected void setDataView(List<HouseRentBean.HouseRentInfo> temp) {

        if (houseRentList.size() == 0) {
            houseRentList = temp;
            return;
        }

        int nowSize = houseRentList.size();
        int newSize = temp.size();
        for (int i = 0; i < newSize; i++) {
            boolean exist = true;
            HouseRentBean.HouseRentInfo mcod = temp.get(i);
            for (int j = 0; j < nowSize; j++) {
                HouseRentBean.HouseRentInfo nmcod = houseRentList.get(j);
                if (mcod.id_.equals(nmcod.id_)) {
                    exist = false;
                    break;
                }
            }
            if (exist)
                houseRentList.add(mcod);

        }
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_sale://出售

               changeSellColor();

                currentStyle = Constant.HOUSESELL;
                sellIndex = 1;
                rentIndex = 1;

                if (houseRentList!=null)
                    houseRentList.clear();

                getHouseList(sellIndex, currentStyle);

                break;
            case R.id.tv_rent://出租

                chageRentColor();

                currentStyle = Constant.HOUSERENT;

                sellIndex = 1;
                rentIndex = 1;

                if (houseRentList!=null)
                    houseRentList.clear();

                getHouseList(rentIndex, currentStyle);

                break;

            case R.id.tv_right: //发布

                showChoose();

                break;

        }
    }

    private void chageRentColor() {

        tv_sale.setBackgroundResource(R.drawable.shape_rent_white_bg_left);
        tv_rent.setBackgroundResource(R.drawable.shape_rent_green_bg_right);
        tv_rent.setTextColor(getResources().getColor(R.color.white));
        tv_sale.setTextColor(getResources().getColor(R.color.text_color));

    }

    private void changeSellColor() {

        tv_sale.setBackgroundResource(R.drawable.shape_rent_green_bg_left);
        tv_rent.setBackgroundResource(R.drawable.shape_rent_white_bg_right);

        tv_sale.setTextColor(getResources().getColor(R.color.white));
        tv_rent.setTextColor(getResources().getColor(R.color.text_color));
    }

    /**
     * 弹出选择照片的对话框
     */
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("我要出售", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 出售
                                Intent mIntent = new Intent(HouseRentActivity.this, SaleReleaseActivity.class);
                                startActivity(mIntent);

                            }

                        })
                .addSheetItem("我要出租", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 出租
                                Intent mIntent = new Intent(HouseRentActivity.this, RentReleaseActivity.class);
                                startActivity(mIntent);

                            }
                        }).show();

    }

    private void lastUpdate(PullToRefreshBase<ListView> refreshView) {

        String label = DateUtils.formatDateTime(HouseRentActivity.this, System.currentTimeMillis(),
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

        refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

    }

}
